package com.lframework.xingyun.basedata.bo.shop;

import cn.hutool.core.util.StrUtil;
import com.lframework.starter.web.core.bo.BaseBo;
import com.lframework.starter.web.core.utils.ApplicationUtil;
import com.lframework.xingyun.basedata.entity.Product;
import com.lframework.xingyun.basedata.entity.ProductBrand;
import com.lframework.xingyun.basedata.entity.ProductCategory;
import com.lframework.xingyun.basedata.entity.ShopProduct;
import com.lframework.xingyun.basedata.service.product.ProductBrandService;
import com.lframework.xingyun.basedata.service.product.ProductCategoryService;
import com.lframework.xingyun.basedata.service.product.ProductService;
import io.swagger.annotations.ApiModelProperty;

import lombok.Data;

/**
 * <p>
 * 门店商品关系表 QueryBo
 * </p>
 *
 */
@Data
public class QueryShopProductBo extends BaseBo<ShopProduct> {

    /**
     * ID
     */
    @ApiModelProperty("ID")
    private String id;

    /**
     * 门店id
     */
    @ApiModelProperty("门店id")
    private String shopId;

    /**
     * 可售商品
     */
    @ApiModelProperty("可售商品")
    private String productId;

    /**
     * 商品类型
     */
    @ApiModelProperty("商品类型")
    private Integer productType;

    /**
     * 商品编码
     */
    @ApiModelProperty("商品编码")
    private String productCode;

    /**
     * 商品名称
     */
    @ApiModelProperty("商品名称")
    private String productName;

    /**
     * SKU编码
     */
    @ApiModelProperty("SKU编码")
    private String skuCode;

    /**
     * 商品简码
     */
    @ApiModelProperty("商品简码")
    private String externalCode;

    /**
     * 单位
     */
    @ApiModelProperty("单位")
    private String unit;

    /**
     * 规格
     */
    @ApiModelProperty("规格")
    private String spec;

    /**
     * 参考销售价（元）
     */
    @ApiModelProperty("参考销售价（元）")
    private String salePrice;

    /**
     * 商品分类id
     */
    @ApiModelProperty("商品分类id")
    private String productCategoryId;

    /**
     * 商品分类名称
     */
    @ApiModelProperty("商品分类名称")
    private String categoryName;

    /**
     * 商品品牌
     */
    @ApiModelProperty("商品品牌Id")
    private String productBrandId;

    /**
     * 商品品牌名称
     */
    @ApiModelProperty("商品品牌名称")
    private String brandName;

    public QueryShopProductBo() {

    }

    public QueryShopProductBo(ShopProduct dto) {

        super(dto);
    }

    @Override
    public BaseBo<ShopProduct> convert(ShopProduct dto) {
        return super.convert(dto);
    }

    @Override
    protected void afterInit(ShopProduct dto) {
        if (StrUtil.isNotBlank(productId)) {
            Product product = ApplicationUtil.getBean(ProductService.class).findById(productId);
            if (product != null) {
                this.productType = product.getProductType().getCode();
                this.productCode = product.getCode();
                this.productName = product.getName();
                this.skuCode = product.getSkuCode();
                this.externalCode = product.getExternalCode();
                this.unit = product.getUnit();
                this.spec = product.getSpec();
                this.productCategoryId = product.getCategoryId();
                ProductCategoryService productCategoryService = ApplicationUtil.getBean(
                        ProductCategoryService.class);
                ProductCategory productCategory = productCategoryService.findById(this.productCategoryId);
                this.categoryName = productCategory.getName();

                this.productBrandId = product.getBrandId();
                ProductBrandService productBrandService = ApplicationUtil.getBean(ProductBrandService.class);
                ProductBrand brand = productBrandService.findById(this.productBrandId);
                this.brandName = brand.getName();
            }
        }
    }
}

